package com.example.front.main.community;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.main.MainActivity;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

/**
 * 房间创建界面类
 */
public class RoomCreateActivity extends AppCompatActivity {
    private String URL;
    private MyApplication myApplication;
    private TextView name_textView;
    private TextView type_textView;
    private TextView price_textView;
    private TextView maxCount_textView;
    private CheckBox state_checkBox;
    private EditText remark_editText;
    private Button create_button;
    private String name = null;
    private String type = null;
    private int maxCount = 0;
    private double cost = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_create);
        name_textView = findViewById(R.id.name_textView);
        type_textView = findViewById(R.id.type_textView);
        price_textView = findViewById(R.id.price_textView);
        maxCount_textView = findViewById(R.id.maxCount_textView);
        state_checkBox = findViewById(R.id.state_checkBox);
        remark_editText = findViewById(R.id.remark_editText);
        create_button = findViewById(R.id.create_button);
        myApplication = (MyApplication)getApplication();
        URL = myApplication.getURL();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        name = bundle.getString("name", "");
        type = bundle.getString("type", "");
        maxCount = bundle.getInt("maxCount");
        cost = bundle.getDouble("cost");
        name_textView.setText(name + "房");
        type_textView.setText(type + "");
        price_textView.setText(cost + "元/时");
        maxCount_textView.setText(maxCount + "人");
    }

    public void create_room_onClick(View view){
        int state = 1;
        if(state_checkBox.isChecked())state = 0;
        String remark = remark_editText.getText().toString();
        SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "");
        OkHttpUtils
                .post()
                .url(URL + "/room/createRoom")
                .addParams("account", account)
                .addParams("name", name)
                .addParams("state", String.valueOf(state))
                .addParams("flag", remark)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                    }
                    @Override
                    public void onResponse(String response) {
                        if(response.equals("true")) {
                            Toast.makeText(RoomCreateActivity.this, "创建成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                        else
                            Toast.makeText(RoomCreateActivity.this, "创建失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
